package com.augmentum.login.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.augmentum.login.Constants;
import com.augmentum.login.dao.ExamDao;
import com.augmentum.login.dao.QuestionDao;
import com.augmentum.login.exception.ParamException;
import com.augmentum.login.exception.ServiceException;
import com.augmentum.login.model.Exam;
import com.augmentum.login.model.Exam_question;
import com.augmentum.login.model.Page;
import com.augmentum.login.service.ExamService;
import com.augmentum.login.util.StringUtil;

@Service("examService")
public class ExamServiceImpl implements ExamService {

    @Resource
    private ExamDao examDao;
    @Resource
    private QuestionDao questionDao;

    @Override
    public void addExam(Exam exam) throws ParamException, ServiceException {

        ParamException param = new ParamException();

        if (StringUtil.isEmpty(exam.getName())) {
            param.addExceptionField(Constants.EXAM_NAME, Constants.NONE_EMPTY_STRING);
        }

        if (param.hasExceptionFiled()) {
            throw param;
        }

        Exam em = examDao.getByName(exam.getName());
        if (em != null) {
            ServiceException se = new ServiceException(1, Constants.NUMBER_EXIST);
            throw se;
        }

        examDao.createExam(exam);
        if (exam.getState()==1) {
            int examId = exam.getId();
            List<Integer> list = questionDao.getQuestionCountForRandom(exam.getQuestionCount());
            List<Exam_question> eq = new ArrayList<Exam_question>();
            for (Integer i : list) {
                eq.add(new Exam_question(examId, i));
            }
            examDao.saveQuestionForBatch(eq);
        }
    }

    @Override
    public void modifyExam(Exam exam) throws ParamException {

        ParamException param = new ParamException();
        if (StringUtil.isEmpty(exam.getBeginTime())) {
            param.addExceptionField(Constants.BEGIN_TIEM, Constants.NONE_EMPTY_STRING);
        }

        if (param.hasExceptionFiled()) {
            throw param;
        }

        if (exam.getChangeCount() > 0) {
            List<Integer> list = questionDao.getQuestionCountForRandom(exam.getChangeCount());
            List<Exam_question> eq = new ArrayList<Exam_question>();
            int id = exam.getId();
            for (Integer i : list) {
                eq.add(new Exam_question(id, i));
            }
            examDao.saveQuestionForBatch(eq);
        } else if (exam.getChangeCount() <0) {
            //examDao.deleteQuestionForBatch(list);
        }
        examDao.updateExam(exam);
    }

    @Override
    public Exam getExamByName(String name) {
        return examDao.getByName(name);
    }

    @Override
    public void removeBathExams(String examStr, String type, int state) {
        examDao.deleteBatchExams(examStr.replaceAll(" ", "").split(type), state);
    }

    @Override
    public List<Exam> searchExamsByPage(String name, String description, String order, Page page) {
        return examDao.findExamsByPage(name, description, order, page);
    }

    @Override
    public List<Exam> searchDraftByPage(String name, Page page) {
        return examDao.findDraftByPage(name, page);
    }

    @Override
    public int getExamCount(String name, String description) {
        return examDao.getExamCount(name, description);
    }

    @Override
    public int getDraftCount(String name) {
        return examDao.getDraftCount(name);
    }

    @Override
    public Exam getExamById(int id) {
        return examDao.getById(id);
    }
}
